/*
  (C) 2007 - Boston University
  Contact: Michael Ocean 

	this file was adapted from a CS511 submission

  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench 

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package stepide;

import java.io.File;
import java.util.Hashtable;
import javax.swing.filechooser.FileFilter;


/**
 * Overview: This is a custom class for the express purpose of filtering the Open dialog to files
 * with the extension XML and the binary format stepstone.
 */
public class StepFileFilter extends FileFilter {
	private String _description = null;
	private Hashtable _filters = null;
	
	public static final int FORMAT_XML = 1;
	public static final int FORMAT_PNG = 2;

	
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
    
	public StepFileFilter(int fileType) {
		_filters = new Hashtable();
		switch(fileType) {
			case FORMAT_XML:
				_filters.put("xml", this);
				_description = "XML Files";
				break;
			case FORMAT_PNG:
				_filters.put("png", this);
				_description = "PNG Images";
				break;
			default:
				throw new IllegalArgumentException("Invalid file type");
		}
	}

	/**
		Effects: Returns true if this file type should be accepted by the dialog
	*/
	public boolean accept(File f) {
		if (f != null) {
			if (f.isDirectory()) {
				return true;
			}

			String extension = getExtension(f);
			if (extension != null && _filters.get(extension) != null) {
				return true;
			}
		}
		return false;
	}

	public String getDescription() {
		return _description;
	}
}
